package com.jie.listnenr;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.jie.entity.Airport;

import com.jie.mapper.AirportMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Slf4j
public class EasyExcelReadListener extends AnalysisEventListener<Airport> {

    @Autowired
  private   AirportMapper airportMapper;

    private List<Airport> airports = new ArrayList<>();

    public EasyExcelReadListener(AirportMapper easyExcelMapper) {
        this.airportMapper = easyExcelMapper;
    }


    @Override
    public void invoke(Airport easyExcel, AnalysisContext analysisContext) {
        log.info("开始读取文章：{}", easyExcel.toString());
       airports.add(easyExcel);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        log.info("进入到：doAfterAllAnalysed  方法中");
        airports.forEach(airport-> {
            airportMapper.insert(airport);
        });
    }
}
